home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 1 / Macwelt DVD 1.toast / Web-Publishing / HTML-Editoren / Alpha ƒ / Tcl / Modes / Matlab mode / matlabHTML.tcl < prev    next >
Encoding:
Text File  |  2000-11-18  |  4.9 KB  |  188 lines

  1. ################################################################################
  2. #
  3. # matlabHTML.tcl, part of the matlab mode package
  4. # This is a set of TCL proc's that can be used to make html documentation 
  5. # for "properly" documented matlab files.  "Properly" is defined to mean they 
  6. # are written so that the matlab command "helpwin" works well for them.
  7. ################################################################################
  8.  
  9. proc matlabHTML.tcl {} {}
  10.  
  11. #############################################################################
  12. # Make html files for a directory of .m files
  13. #############################################################################
  14.  
  15. proc makeMatlabHTML {} {
  16.     global matlabBusy
  17.     
  18.     if {$matlabBusy} {
  19.         alertnote "Matlab is busy, try again later."
  20.         return
  21.     }
  22.     
  23.     set codeDir [get_directory -p "Select folder:"]
  24.     if {![catch {glob -types TEXT -dir $codeDir -- "*.m"} filelist]} {
  25.         set filelist [makeMatlabEmptyHTML $filelist]
  26.         foreach mFile $filelist {
  27.             makeMatlabHtmlFile $mFile
  28.         }
  29.         
  30.     } else {
  31.         alertnote "No .m files found in $codeDir"
  32.     }
  33. }
  34.  
  35. ############################################################################# 
  36. # Make empty html files for a directory of .m files.
  37. # We do this to check if old ones should be overwritten and to make sure 
  38. # "see also" links work properly .
  39. # Returns a subset of filelist to be further processed
  40. #############################################################################
  41.  
  42. proc makeMatlabEmptyHTML {filelist} {
  43.     set overwrite "ask"
  44.     set newFileList [list]
  45.     foreach mFile $filelist {
  46.     
  47.     set codeDir [file dirname $mFile]
  48.     set root [file rootname [file tail $mFile]]
  49.     
  50.     set htmlDir [file join $codeDir html]
  51.     if {![file isdirectory $htmlDir]} {file mkdir $htmlDir}
  52.     set htmlFile [file join $htmlDir $root.html]
  53.     
  54.     if {[file exists $htmlFile]} {
  55.         if {$overwrite == "ask"} {
  56.         set overwrite [askyesno -c "Some html files already exist.  Should I replace them?  Old files will be put in trash."]
  57.         }
  58.         switch -- $overwrite {
  59.         "yes" {
  60.             regexp -- {^[^:]*} $htmlFile theDisk
  61.             if {[catch {file rename "$htmlFile" [file join $theDisk Trash]}]} {
  62.             alertnote "Problem moving old files to trash.  Try emptying the trash."
  63.             return
  64.             }
  65.             close [open $htmlFile w]
  66.             lappend newFileList $mFile
  67.         }
  68.         "no" {}
  69.         "cancel" {return ""}
  70.         }
  71.     } else {
  72.         close [open $htmlFile w]
  73.         lappend newFileList $mFile
  74.     }
  75.     }
  76.     return $newFileList
  77. }
  78.  
  79.  
  80. #############################################################################
  81. # Parse the comment header of an .m file and make a .html file
  82. #############################################################################
  83.  
  84. proc makeMatlabHtmlFile {mFile} {
  85.     
  86.     message "Processing $mFile"
  87.     
  88.     #
  89.     # Figure out where the files are and open them
  90.     #
  91.     
  92.     set codeDir [file dirname $mFile]
  93.     set root [file rootname [file tail $mFile]]
  94.     
  95.     set htmlDir [file join $codeDir html]
  96.     if {![file isdirectory $htmlDir]} {file mkdir $htmlDir}
  97.     set htmlFile [file join $htmlDir $root.html]
  98.     
  99.     set mFileID [open $mFile r]
  100.     set htmlFileID [open $htmlFile w]
  101.     
  102.     #
  103.     # Get the function declaration or script name
  104.     #
  105.     
  106.     gets $mFileID firstLine
  107.     if {![regexp -- {([ \t]*)(function)(.*)} $firstLine allText cc function call]} {
  108.         set call $root
  109.         seek $mFileID 0 start
  110.     }
  111.     
  112.     #
  113.     # Scan the header
  114.     #
  115.     
  116.     set text ""
  117.     set seeAlso ""
  118.     while {[gets $mFileID oneLine] != -1} {
  119.         if {![regexp -- {(%)(.*)} $oneLine allText cc comment]} {break}
  120.         if {[regexp -- {(%)([\t ]*See also:)(.*)} $oneLine allText cc sa allSee]} {
  121.             set comment "$sa [parceMatlabSeeAlso $allSee]"
  122.         }
  123.         append text "$comment\r"
  124.     }
  125.     
  126.     #
  127.     # Write the .html file
  128.     #
  129.     
  130.     puts $htmlFileID "<HTML>\r<HEAD>"
  131.     puts $htmlFileID "<TITLE>$root</TITLE>"
  132.     puts $htmlFileID "</HEAD>"
  133.     puts $htmlFileID "<BODY BGCOLOR=#F0F0F0>\r"
  134.     puts $htmlFileID "<H1>$root</H1>\r"
  135.     puts $htmlFileID "<HR>\r"
  136.     puts $htmlFileID "<H2>$call</H2>\r"
  137.     puts $htmlFileID "<PRE>$text\r</PRE>\r"
  138.     puts $htmlFileID "<HR><BR>\r"
  139.     
  140.     #
  141.     # Add helpdesk and contents to footer
  142.     #
  143.     
  144.     regsub  -all ":" [matlabHelpDir] "/" helpDesk
  145.     set helpDesk "file:///$helpDesk/helpdesk.html"
  146.         
  147.     set contents [file tail [file dirname $mFile]]
  148.     set footer "<P ALIGN=\"CENTER\">\["
  149.     append footer "<A HREF=\"contents.html\">$contents</A> | "
  150.     append footer "<A HREF=\"$helpDesk\">Help Desk</A>\]</P>\r"
  151.     
  152.     puts $htmlFileID "$footer\r</BODY>\r</HTML>"
  153.  
  154.     #
  155.     # All done, so close files
  156.     #
  157.     
  158.     close $mFileID
  159.     close $htmlFileID
  160.     message "Finished writing $htmlFile"
  161.     
  162. }
  163.  
  164.  
  165. #############################################################################
  166. # Parse a see also line
  167. #############################################################################
  168.  
  169. proc parceMatlabSeeAlso {allSee} {
  170.     set seeAlso ""
  171.     set pre " "
  172.     while {[regexp -- {(\b[_\w]+\b)(.*)} $allSee aText oneSee allSee]} {
  173.         set docURL [findMatlabHelpFile $oneSee]
  174.         if {$docURL != ""} {
  175.             set oneLink "<A HREF=\"$docURL\">$oneSee</A>"
  176.         } else {
  177.             set oneLink "$oneSee"
  178.         }
  179.         append seeAlso "$pre$oneLink"
  180.         set pre ", "
  181.     }
  182.     return $seeAlso
  183. }
  184.